<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="openAssignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :total="query.total" :current-page="query.page" :page-size="query.pagesize" layout="prev,pager,next" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 增加角色对话框 -->
    <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible.sync=" showDialog" @close="close">
      <el-form ref="fm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog = false">取消</el-button>
          <el-button size="small" type="primary" @click="addRole">确定</el-button>
        </el-col>
      </el-row>
      +
    </el-dialog>
    <!-- 给角色添加权限点弹框 -->
    <assignPerm ref="ap" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import assignPerm from './components/assign-perm.vue'

export default {
  components: {
    assignPerm
  },
  data () {
    return {
      showAssignDialog: false,
      list: [],
      query: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名字长度为2-10', trigger: 'blur' }],
        description: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {

    // 给角色分配权限
    openAssignPerm (id) {
      this.showAssignDialog = true
      this.$refs.ap.getRolePerm(id)
    },

    async getList () {
      const { total, rows } = await getRoleList(this.query)
      // console.log(rows)
      this.query.total = total
      this.list = rows
    },
    changePage (page) {
      // console.log(page)
      // 更新页码
      this.query.page = page
      // 刷新列表
      this.getList()
    },
    async delRole (role) {
      try {
        await this.$confirm(`确定要删除${role.name}角色吗？`)
        await deleteRole(role.id)
        this.getList()
        this.$message.success('删除成功！')
      } catch (error) {
        console.log(error)
      }
    },
    close () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.fm.resetFields()
      // 上面visible加sync同步就不用再写这句
      // this.showDialog = false
    },
    async addRole () {
      try {
        await this.$refs.fm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
          this.getList()
          this.$message.success('编辑角色成功！')
          this.showDialog = false
        } else {
          await addRole(this.roleForm)
          this.query.page = 1
          this.getList()
          this.showDialog = false
          this.$message.success('添加角色成功！')
        }
      } catch (error) {
        console.log(error)
      }
    },
    async editRole (id) {
      const res = await getRoleDetail(id)
      console.log(res)
      this.roleForm = res
      this.showDialog = true
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
